#include <simplemq/simplemq_redis.hpp>
#include <date_utilits.h>
#include "BaseCmdParam.h"
#include "person.hpp"
/************************************************************************/
/* REDIS 发布测试                                                       */
/************************************************************************/
using namespace simplemq;
// 命令行参数解析器
class ClientConfig : public gdface::BaseClientConfig {
public:
	const std::string PARAM_CHANNEL_NAME = "channel";
	const std::string PARAM_AUTH = "auth";
	/** 频道名，默认'c1' */
	std::string channel;
	std::string password;
	ClientConfig() :gdface::BaseClientConfig(), channel(), password() {
		cmdparam.add<std::string>(PARAM_CHANNEL_NAME, 'c', "channel name for subscribing", false, "c1");
		cmdparam.add<std::string>(PARAM_AUTH, 'a', "password for authorization", false, "");
	}
	~ClientConfig() = default;
	void afterParse() {
		gdface::BaseClientConfig::afterParse();
		channel = cmdparam.get<std::string>(PARAM_CHANNEL_NAME);
		password = cmdparam.get<std::string>(PARAM_AUTH);
	}
};

std::string now_str() {
	auto now = std::chrono::system_clock::now();
	auto mills = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
	return date::format_iso8601(mills);
}

int
main(int argc, char *argv[]) {
	ClientConfig param;
	param.parse(argc, argv);
	ClientManager cmgr;
	try {
#if 0
		auto rm = cmgr.make_client_manager(param.host, param.port, param.password);
		auto pub = std::make_shared<redis_publisher>(rm);
		for (int i = 0; i < 100; ++i) {
			// 
			auto c = pub->publish(param.channel, now_str().append(" ").append(std::to_string(i)));
			SAMPLE_LOG("RECEIVE CLIENT {}", c);
		}
#else
		auto rm = cmgr.make_client_manager(mq_client_params{ param.host, param.port, param.password });
		// 创建一个person类型的发布实例，用于将person对象序列化后发布到redis频道
		auto ipub = cmgr.getPublisher(rm);
		for (int i = 0; i < 5; ++i) {
			person p = { "tom",23,i };
			// 发布单个对象
			auto c = ipub->publish(param.channel, p);
			SAMPLE_LOG("RECEIVE CLIENT {}", c);
		}
		// 发布一组对象
		std::vector<person> v = { {"tom",22,1},{ "jerry",21,2 },{"grace",12,3} };
		ipub->publish<person>(param.channel, v.begin(), v.end());
#endif
	}
	catch (const std::exception &e)
	{
		SAMPLE_ERR("{}:{}", typeid(e).name(), e.what());
	}
}
